package com.wally.service;

import java.util.UUID;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import com.wally.entity.QueueMsg;

@Service
public class Producer {

	// 也可以注入JmsTemplate，JmsMessagingTemplate对JmsTemplate进行了封装
	@Autowired
	private JmsTemplate jmsTemplate;

	// 发送消息，destination是发送到的队列，message是待发送的消息
	public void sendMessage() {

		String queueName = "test";
		QueueMsg msg = new QueueMsg(UUID.randomUUID().toString(), "sms");

		jmsTemplate.setPriority(1);
		jmsTemplate.send(queueName, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				ObjectMessage objectMessage = session.createObjectMessage(msg);
				objectMessage.setJMSPriority(1);
				return objectMessage;
			}
		});
	}
}